/**
 *	多选框组件V1.0.1 
 * 	suyao
 */
srvMap.add('queryMultiselect', 'multiSelectInfo.json','front/sh/indicatorStore!execute?uid=store_003');

//是否有更多选项
var HVAE_MORE = "1";
var NO_HAVE_MORE = "0";

//展示类型
var SINGLE_SELECT = "1";   //单选框
var MULTI_SELECT = "2";    //多选框
var TEXT_BOX = "3";        //文本框
var SINGLE_DATE = "4";     //单个时间
var DATE_FRAME = "5";	   //时间范围
var MULTI_SELECT_NUM;        //多选类型个数

var EXHIBITION_NUM = 2;   //默认展示数量

var tipIdArray = [];
var tipNameArray = [];


/**
 * 多选框
 * @author suyao
 */
Multi_Select = {
		queryMultiselect :function(){
			var par = "";
			Util.ajax.postJsonSync(srvMap.get('queryMultiselect'),par,function(json,status){
				if(status){
					var selectInfoArray = Multi_Select.optimizeJson(json.object);
					Util.ajax.loadTemp('.selectBox',$('#IDX_MULTI_SELECT'),selectInfoArray);
				}else{
					$.messager.alert('错误','数据异常！'); 
				}
			});
		},
		optimizeJson : function(obj){
			var selectInfoArray = [];
			var objLength = obj.FOLDER_INFO.length;
			MULTI_SELECT_NUM = objLength;
			for(var i = 0;i<objLength;i++){
				var selectInfoObj = new Object();
				selectInfoObj.key = obj.FOLDER_INFO[i].masterTableColumn;
				selectInfoObj.moreFlag = obj.FOLDER_INFO[i].moreFlag;
				selectInfoObj.moreValue = obj.FOLDER_INFO[i].moreValue;
				selectInfoObj.displayCode = obj.FOLDER_INFO[i].displayCode;
				selectInfoObj.id = obj.FOLDER_INFO[i].tableOtherName;
				selectInfoObj.name = obj.FOLDER_INFO[i].quryDesc;
				if(obj.FOLDER_INFO[i].displayCode == SINGLE_SELECT || obj.FOLDER_INFO[i].displayCode == MULTI_SELECT){
					selectInfoObj.dimInfoList = obj.FOLDER_INFO[i].dimInfoList;
					if(obj.FOLDER_INFO[i].moreFlag == HVAE_MORE){   //有"更多"设置
						var moreValue = obj.FOLDER_INFO[i].moreValue;   //初始展示的个数
						var lessDimInfoList = obj.FOLDER_INFO[i].dimInfoList.slice(0,moreValue);
						selectInfoObj.lessDimInfoList = lessDimInfoList;
					}
				}
				selectInfoArray.push(selectInfoObj);
			}
			return selectInfoArray;
		},
		moreDownAndUp : function(obj){
			//tip上显示的元素数组
			var arrayTemp1 = [];
			$('#selectTip').find("li").each(function(){
				var value = $(this).attr('value');
				arrayTemp1.push(value);
			});
			
			//选择框中元素数组
			var arrayTemp2 = [];
			$(".selectBox").find("li").each(function(){
				var value = $(this).attr("id");
				arrayTemp2.push(value);
			});
			
			var className = $(obj).attr("class");
			if(className == "moreup"){
				//收回
				$(obj).attr("class","");
				//获取要隐藏的数组
				var arrayTemp3 = [];
				var arrayTemp4 = [];
				for(var n = 0;n<arrayTemp2.length;n++){
					if(n<EXHIBITION_NUM){
						arrayTemp4[n] = arrayTemp2[n];
					}else{
						arrayTemp3[n-EXHIBITION_NUM] = arrayTemp2[n];
					}
				}
				
				var arrayTemp5 = [];
				arrayTemp5 = Multi_Select.arrayGetRepeat(arrayTemp1,arrayTemp4);
				
				for(var i=0;i<arrayTemp3.length;i++){
					$('#'+arrayTemp3[i]+'').hide();
				}
				for(var j=0;j<arrayTemp5.length;j++){
					$('#'+arrayTemp5[j]+'').hide();
				}
			}else{
				//如果按钮显示为更多
				//1.展示全部2.隐藏与tip重复的元素
				
				//俩个数组进行取重
				var arrayTemp3 = [];
				arrayTemp3 = Multi_Select.arrayGetRepeat(arrayTemp1,arrayTemp2);
				
				$(obj).attr("class","moreup");
				for(var i=0;i<MULTI_SELECT_NUM;i++){
					$(".selectBox").find("li").eq(i).show();
				}
				
				var arrayTempLength = arrayTemp3.length;
				for(var i=0;i<arrayTempLength;i++){
					$('#'+arrayTemp3[i]+'').hide();
				}
			}
		},
		/**
		 * 多选框更多、收回切换
		 * @param obj
		 * @param id
		 * @param type
		 */
		changeMuliShowMode : function(obj,id,type){
			var moreFlag = $(obj).attr("value");
			if(moreFlag  == HVAE_MORE){ 
				//更多设置显示
				var a = "#select"+id+"";
				$("#select"+id+"").hide();
				if(type == 1){  //更多
					$("#btnMoreSel"+id+"").hide();
					$("#btnLessSel"+id+"").show();
					
					$("#lessSelect"+id+"").hide();
					$("#moreSelect"+id+"").show();
					
					if($("#btnMultiSel"+id+"").css("display")=='none'){   //处于多选状态
						$("#singleMoreSel"+id+"").hide();
						$("#multiMoreSel"+id+"").show();
					}else{   //处于单选状态
						$("#singleMoreSel"+id+"").show();
						$("#multiMoreSel"+id+"").hide();
					}
					
				}else{  //收回
					$("#btnMoreSel"+id+"").show();
					$("#btnLessSel"+id+"").hide();
					
					$("#lessSelect"+id+"").show();
					$("#moreSelect"+id+"").hide();
					
					if($("#btnMultiSel"+id+"").css("display")=='none'){   //处于多选状态
						$("#singleLessSel"+id+"").hide();
						$("#multiLessSel"+id+"").show();
					}else{   //处于单选状态
						$("#singleLessSel"+id+"").show();
						$("#multiLessSel"+id+"").hide();
					}
				}
			}
		},
		/**
		 * 多选框多选、单选
		 * @param obj
		 * @param id
		 * @param type
		 */
		changeMuliSelectMode : function(obj,id,type){
			var moreFlag = $(obj).attr("value");
			if(moreFlag == HVAE_MORE){   //更多设置
				$("#select"+id+"").hide();
				//检查当前是更多状态还是收回状态
				if($("#btnMoreSel"+id+"").css("display")=='none'){ 
					//处于更多状态
					$("#moreSelect"+id+"").show();
					$("#lessSelect"+id+"").hide();
					if(type == 1){  
						//按钮变换样式，点击多选按钮
						$("#btnMultiSel"+id+"").hide();
						$("#btnSingleSel"+id+"").show();
						
						//更多+多选
						$("#singleMoreSel"+id+"").hide();
						$("#multiMoreSel"+id+"").show();
						
					}else{
						//按钮变换样式，点击单选按钮
						$("#btnMultiSel"+id+"").show();
						$("#btnSingleSel"+id+"").hide();
						
						//更多+单选
						$("#singleMoreSel"+id+"").show();
						$("#multiMoreSel"+id+"").hide();
					}
				}else{   
					//处于收回状态
					$("#moreSelect"+id+"").hide();
					$("#lessSelect"+id+"").show();
					if(type == 1){
						//按钮变换样式，点击多选按钮
						$("#btnMultiSel"+id+"").hide();
						$("#btnSingleSel"+id+"").show();
						
						//收回+多选
						$("#singleLessSel"+id+"").hide();
						$("#multiLessSel"+id+"").show();
					}else{
						//按钮变换样式，点击单选按钮
						$("#btnMultiSel"+id+"").show();
						$("#btnSingleSel"+id+"").hide();
						
						//收回+单选
						$("#singleLessSel"+id+"").show();
						$("#multiLessSel"+id+"").hide();
					}
				}
			}else{   //无更多
				if(type == 1){
					//按钮变换样式，点击多选按钮
					$("#btnMultiSel"+id+"").hide();
					$("#btnSingleSel"+id+"").show();
					
					//多选
					$("#singleSelect"+id+"").hide();
					$("#multiSelect"+id+"").show();
				}else{
					//按钮变换样式，点击单选按钮
					$("#btnMultiSel"+id+"").show();
					$("#btnSingleSel"+id+"").hide();
					
					//单选
					$("#singleSelect"+id+"").show();
					$("#multiSelect"+id+"").hide();
				}
			}
		},
		/**
		 * 单选更多、收回
		 * @param obj
		 * @param id
		 * @param type
		 */
		changeLessShowMode : function(obj,id,type){
			var moreFlag = $(obj).attr("value");
			if(moreFlag == HVAE_MORE){
				//有更多设置
				$("#select"+id+"").hide();
				if(type == 1){
					//点击更多按钮
					$("#lessSelect"+id+"").hide();
					$("#moreSelect"+id+"").show();
					
					$("#btnMoreSel"+id+"").hide();
					$("#btnLessSel"+id+"").show();
				}else{
					//点击收回按钮
					$("#lessSelect"+id+"").show();
					$("#moreSelect"+id+"").hide();
					
					$("#btnMoreSel"+id+"").show();
					$("#btnLessSel"+id+"").hide();
				}
			}
		},
		/**
		 * 添加小标签
		 */
		addSelectTip : function(id,jsonName,name,key,title,flag){
			var strHtml ="";
			strHtml = "<li class='classifySelect' value='"+jsonName+"'>";
			strHtml += '<p id="'+id+'" value="'+key+'">'+title+''+name+'</p>';
			strHtml += '<span value="'+flag+'" onclick="Multi_Select.delSelectTip(this,1);"></span>';
			strHtml += "</li>";
			$('#selectTip').append(strHtml);
			//查询信息方法
			Idx_Store.init();
		},
		/**
		 * 删除小标签
		 * @param obj
		 */
		delSelectTip : function(obj,flag){
			var jsonName = $(obj).parent().attr("value");
			$('#'+jsonName+'').show();
			$(obj).parent().remove();
			
			//更多选项收回
			//TODO
			$(".moreDown").find("em").attr("class","");
			
			if(flag == "0"){
				tipIdArray = [];
				tipNameArray = [];
			}
			
			//TODO
			//查询信息方法
			Idx_Store.init();
		},
		/**
		 * 单选点击选择
		 * @param obj
		 */
		selectSingleTip : function(obj){
			var id = $(obj).attr('value');   //被选择元素的id
			var jsonName = $(obj).parent().parent().attr("value");   //唯一表示，将作为jsonname
			var name = $(obj).text();        //json中name值
			var key = $("#"+jsonName+"").find('strong').attr('value');   //json中key值
			var title = $("#"+jsonName+"").find('strong').text();        //展示title
			
			$("#"+jsonName+"").hide();   //本行隐藏
			
			Multi_Select.addSelectTip(id,jsonName,name,key,title,0);
		},
		/**
		 * 多选点击提交
		 * @param obj
		 * @param jsonName
		 */
		selectMoreTip : function(obj,jsonName){
			var key = $("#"+jsonName+"").find('strong').attr('value');   //json中key值
			var title = $("#"+jsonName+"").find('strong').text();        //展示title
			var id = "";
			var value= "";
			var $label = $(obj).parent().parent().find("label");
			$label.each(function(){
				var labelId = "";
				var labelvalue = "";
				if($(this).find("input").is(':checked')){
					labelId = $(this).find("input").val();
					labelvalue = $(this).text();
					
					id += labelId +",";
					value += labelvalue + ",";
				}
			});
			id = id.substring(0,id.length-1);
			value = value.substring(0,value.length-1);
			
			$("#"+jsonName+"").hide();
			
			Multi_Select.addSelectTip(id,jsonName,value,key,title,0);
		},
		/**
		 * 单个文本框提交
		 * @param obj
		 * @param inputId
		 * @param jsonName
		 */
		submitSingleInput : function(obj,inputId,jsonName){
			var value = $("#"+inputId+"").val();
			var id = value;
			var key = $("#"+jsonName+"").find('strong').attr('value');   //json中key值
			var title = $("#"+jsonName+"").find('strong').text();        //展示title
			
			$("#"+jsonName+"").hide();
			
			Multi_Select.addSelectTip(id,jsonName,value,key,title,0);
		},
		/**
		 * 时间段提交
		 * @param jsonName
		 */
		submitDateInput : function(jsonName){
			var key = $("#"+jsonName+"").find('strong').attr('value');   //json中key值
			var title = $("#"+jsonName+"").find('strong').text();        //展示title
			
			var startDate = $("#startDate"+jsonName+"").val();
			var endDate = $("#endDate"+jsonName+"").val();
			
			var value = startDate+"~"+endDate;
			var id = startDate+"~!~"+endDate;
			
			$("#"+jsonName+"").hide();
			
			Multi_Select.addSelectTip(id,jsonName,value,key,title,1);
		},
		/**
		 * 所有分类选择
		 * @param obj
		 */
		selectClassTip : function(obj){
			var key = "find_in_set(base_index_info.folder_id,'{parm}')>0";    //写死
		//	var key =  "{parm} like base_index_info.folder_id";
			var title = "分类:";
			var value = $(obj).attr("value2");
			var id = $(obj).attr('id');
			
			var jsonName = "selectClass";   //写死
			
			var tagName = title + value;
//			var tagId = id;
			var tagId = Load_Folder.selectFolderChildId(id);
			
			//获取分类及其子节点Id，拼接成字符串，并且去重
//			Multi_Select.getIdStrBySelect();
			
			//对俩数组进行去重
//			var flag = 0;
//			for(var i=0;tipIdArray.length>i;i++){
//				if(tipIdArray[i] == id){
//					flag += 1;
//				}else{
//					flag += 0;
//				}
//			}
//			if(flag == 0){
//				tipIdArray.push(id);
//				tipNameArray.push(value);
//			}
//			
//			var tipId = "";
//			var tipValue = "";
//			for(var j=0;tipIdArray.length>j;j++){
//				tipId += tipIdArray[j]+',';
//				tipValue += tipNameArray[j]+',';
//			}
//			
//			tipId = tipId.substring(0,tipId.length-1);
//			tipValue = tipValue.substring(0,tipValue.length-1);
//			
			$('#selectTip').find('li').each(function(){
				if($(this).attr('value') == "selectClass"){
					$(this).remove();
				}
			});
//			Multi_Select.addSelectTip(tipId,jsonName,tipValue,key,title,0);
			

			
			var strHtml ="";
			strHtml = "<li class='classifySelect' value='"+jsonName+"'>";
			strHtml += '<p id="'+tagId+'" value="'+key+'">'+tagName+'</p>';
			strHtml += '<span value="'+0+'" onclick="Multi_Select.delSelectTip(this,0);"></span>';
			strHtml += "</li>";
			$('#selectTip').append(strHtml);
			//查询信息方法
			Idx_Store.init();
			
			$('.subsety_tabInd').css("display","none");
//			alert($('.subsetx_tabInd').attr("style"))
//			$('.subsetx_tabInd').hide();
//			alert(11)
		},
		/**
		 * 俩个数组进行获取重复
		 * @param array1
		 * @param array2
		 * @returns {Array}
		 */
		arrayGetRepeat : function(array1,array2){
			var arraytemp = [];
			var array1Length = array1.length;
			var array2Length = array2.length;
			for(var i=0;i<array1Length;i++){
				for(var j=0;j<array2Length;j++){
					if(array1[i] == array2[j]){
						arraytemp.push(array2[j]);
					}
				}
			}
			return arraytemp;
		}
}

/**
 * 页面加载
 */
$(document).ready(function(){
	Multi_Select.queryMultiselect(); 
	
	//默认展示前3个
	for(var i =0;i<EXHIBITION_NUM;i++){
		$(".selectBox").find("li").eq(i).show();
	}
});
